
# from django.shortcuts import render, HttpResponse
from django.views import View
from django_redis import get_redis_connection
from django import http
from verification.libs.captcha.captcha import captcha

class ImageCodeViews(View):
    '''图形验证码'''
    def get(self, request, uuid):
        """
        :param request:
        :param uuid: 通用唯一识别码，用于唯一识别该图形码属于哪一个用户
        :return: image/jpg
        """
        # 实现主体业务逻辑：生成，保存，响应图形验证码
        # 生成验证码
        text, image = captcha.generate_captcha()
        # 保存验证码
        redis_conn = get_redis_connection('verify_code')
        # redis_conn.setex('key', 'expire', 'value') 设置验证码过期时间
        redis_conn.setex('img_%s' % uuid, 120, text)
        # 响应验证码
        return http.HttpResponse(image, content_type='image/jpg')

